package com.wave.wavesystem.server.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.redis.util.RedisUtil;
import com.wave.wavesystem.business.mapper.UserMapper;
import com.wave.wavesystem.business.pojo.entity.UserEntity;
import com.wave.wavesystem.common.constant.RedisKeyConstant;
import com.wave.wavesystem.common.util.*;
import com.wave.wavesystem.server.mapper.ArticleMapper;
import com.wave.wavesystem.server.pojo.dto.ArticleQueryRequest;
import com.wave.wavesystem.server.pojo.entity.ArticleEntity;
import com.wave.wavesystem.server.pojo.vo.article.ArticleHomeCardVO;
import com.wave.wavesystem.server.pojo.vo.article.ArticleServerTableVO;
import com.wave.wavesystem.server.service.ArticleService;
import com.wave.wavesystem.server.wrapper.ArticleWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import wave.common.pojo.enums.RStatusEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-02-23
 * @description 文章表 业务层接口
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ArticleEntity> implements ArticleService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ArticleWrapper articleWrapper;

    @Override
    public Page<ArticleServerTableVO> adminPage(Page<ArticleEntity> page, QueryWrapper queryWrapper) {
        Page<ArticleServerTableVO> vo = new Page<>();
        Page<ArticleEntity> articleEntityPage = this.page(page, queryWrapper);
        if (Func.isEmpty(articleEntityPage.getRecords())) return vo;
        BeanUtils.copyProperties(articleEntityPage, vo);
        List<ArticleServerTableVO> voRecords = articleEntityPage.getRecords().stream().map(item -> {
            ArticleServerTableVO voItem = new ArticleServerTableVO();
            String userName = redisUtil.strGet(RedisKeyConstant.USER_ID_NAME, item.getId().toString(), String.class);
            if (Func.isEmpty(userName)) {
                UserEntity user = userMapper.selectOneById(item.getAuthorId());
                if (Func.isNotEmpty(user)) {
                    redisUtil.strSet(RedisKeyConstant.USER_ID_NAME, user.getId().toString(), user.getName());
                    voItem.setAuthorName(user.getName());
                }
            }
            BeanUtils.copyProperties(item, voItem);
            return voItem;
        }).toList();
        vo.setRecords(voRecords);
        return vo;
    }

    @Override
    public ArticleEntity mySave(ArticleEntity article) {
        // 新增文章
        if (Func.isEmpty(article.getId())) {
            UserEntity user = AuthUtil.getUser();
            ThrowUtil.throwIf(Func.isEmpty(user), RStatusEnum.FAIL);
            article.setAuthorId(user.getId());
        }
        this.save(article);
        return article;
    }

    @Override
    public Page<ArticleHomeCardVO> getHomeCardPage(ArticleQueryRequest articleQueryRequest) {
        ArticleEntity article =new ArticleEntity();
        BeanUtils.copyProperties(articleQueryRequest, article);
        Page<ArticleEntity> articlePage = this.page(Condition.getPage(articleQueryRequest), Condition.getQueryWrapper(article));
        if (Func.isEmpty(articlePage.getRecords())) return new Page<>();
        Page<ArticleHomeCardVO> resPage = new Page<>();
        BeanUtils.copyProperties(articlePage, resPage);
        List<ArticleHomeCardVO> resRows = articleWrapper.pageVO(articlePage).getRecords().stream().map(item -> {
            ArticleHomeCardVO vo = new ArticleHomeCardVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).toList();
        resPage.setRecords(resRows);
        return resPage;
    }

}
